package com.yunc.modules.deploy.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrSpliter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.utils.R;
import com.yunc.modules.deploy.entity.DeployApp;
import com.yunc.modules.deploy.entity.DeployServer;
import com.yunc.modules.deploy.service.IDeployAppService;
import com.yunc.modules.deploy.service.IDeployServerService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部署应用表 前端控制器
 *
 * @author concong tao
 * @since 2020-03-11
 */
@RestController
@RequestMapping("/deployApp")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DeployAppController {

    private final IDeployAppService deployAppService;
    private final IDeployServerService deployServerService;

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermi('deploy:app:list')")
    public R page(Page<DeployApp> page, DeployApp deployApp) {
        QueryWrapper<DeployApp> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(deployApp.getName())) {
            qw.like("name", deployApp.getName());
        }
        qw.orderByDesc("create_time");
        deployAppService.page(page, qw);
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    public R list(){
        QueryWrapper<DeployApp> qw = new QueryWrapper<>();
        List<DeployApp> appList = deployAppService.list(qw);
        QueryWrapper<DeployServer> serverQw = new QueryWrapper<>();
        List<DeployServer> serverList = deployServerService.list(serverQw);
        return R.ok().put("appList", appList).put("serverList", serverList);
    }

    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPermi('deploy:app:list')")
    public R info(@PathVariable String id) {
        DeployApp deployApp = deployAppService.getById(id);
        return R.ok().put("deployApp", deployApp);
    }

    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('deploy:app:save')")
    public R save(DeployApp deployApp) {
        boolean flag = deployAppService.save(deployApp);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('deploy:app:update')")
    public R update(DeployApp deployApp) {
        boolean flag = deployAppService.updateById(deployApp);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/delete/{ids}")
    @PreAuthorize("@ss.hasPermi('deploy:app:delete')")
    public R delete(@PathVariable String ids) {
        List<String> idsList = StrSpliter.splitTrim(ids, ',', true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        boolean flag = deployAppService.removeByIds(idsList);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}